package afcp.alumni.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import afcp.alumni.action.base.MyActionBeanContext;
import afcp.alumni.dao.MemberDao;
import afcp.alumni.model.db.Member;

@Component
public class LoginService {
	
	LoginService() {}

	private static final Log logger = LogFactory.getLog(LoginService.class);
	public static final String ROLE_VIP = "VIP";
	public static final String ROLE_MEMBER = "MEMBER";
	public static final String ROLE_ADMIN = "ADMIN";

	@Autowired
	private MemberDao memberDao;
//	private MemberDaoImpl memberDao = MemberDaoImpl.getDao();
	
	public Member findUser(String login) {
		return memberDao.find(login);
	}

	public boolean login(Member userLoging, MyActionBeanContext context) {
		if (userLoging == null) {
			logger.warn("User loging is null!");
			return false;
		}
		logger.debug("login : " + userLoging.getId());
		context.setUser(userLoging);
		
		logger.info("Authentication succeeded: " + userLoging.getId());
		return true;
	}

	public boolean logout(MyActionBeanContext context) {
		logger.info("Logout: " + (context.getUser() != null ? context.getUser().getId():"unknown user (not in session)"));
		context.removeUser();
		return true;
	}

}
